package cn.edu.gdu.quizpractice.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: Aubery
 * @Date: 2025-01-29-17:32
 * @Description: 返回结果封装类
 */

public class ApiResult<T> {
    /**
     * 错误码，表示一种错误类型
     * 请求成功，状态码为200
     */
    private Integer code;

    /**
     * 对错误代码的详细解释
     */
    private String message;

    /**
     * 返回的结果包装在value中，value可以是单个对象
     */
    private T data;

    public ApiResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResult() {
    }


    /**
     * 携带数据的操作成功
     */
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(200, "操作成功", data);
    }

    /**
     * 携带数据的操作成功
     */
    public static <T> ApiResult<T> success() {
        return new ApiResult<>(200, "操作成功", null);
    }

    /**
     * 操作失败
     */
    public static <T> ApiResult<T> fail(String message) {
        return new ApiResult<>(400, message, null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
